package woaini.wind.bot.core.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import lombok.experimental.UtilityClass;


@UtilityClass
public class XmlTool {

  private final ObjectMapper jsonMapper = new ObjectMapper();
  private final XmlMapper xmlMapper = new XmlMapper();

  public String convertXmlToJson(String xml){
    try {
      // 首先将XML字符串解析为Java对象
      Object xmlObject = xmlMapper.readValue(xml, Object.class);
      // 然后将Java对象转换为JSON字符串
      return jsonMapper.writeValueAsString(xmlObject);
    } catch (JsonProcessingException e) {
      return null;
    }
  }
  public static void main(String[] args) throws Exception {
    String xml =
      "<msgsource>\n    <atuserlist>\n        <![CDATA[,wxid_v85yukiizzag22]]>\n    </atuserlist>\n    <pua>1</pua>\n    <silence>0</silence>\n    <membercount>2</membercount>\n    <signature>V1_qFHfcRIH|v1_qFHfcRIH</signature>\n    <tmp_node>\n        <publisher-id />\n    </tmp_node>\n    <sec_msg_node>\n        <alnode>\n            <fr>1</fr>\n        </alnode>\n    </sec_msg_node>\n</msgsource>\n";
    xml = xml.replaceAll(" ", "").replace("\n","");
    System.out.println(xml);
    System.out.println(convertXmlToJson(xml));
  }

}
